{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "import torch\n",
    "\n",
    "\n"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 创建一个需要求导的变量\n",
    "x = torch.tensor([2.0], requires_grad=True)\n",
    "\n",
    "# 定义计算\n",
    "y = x ** 2   # y = x²\n",
    "\n",
    "# 反向传播（自动求导）\n",
    "y.backward()\n",
    "\n",
    "# 查看梯度\n",
    "print(x.grad)\n",
    "\n"
   ],
   "id": "12b62c7e7bbc81bb",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)\n",
    "y = (x ** 3).sum()\n",
    "y.backward()\n",
    "print(x.grad)\n",
    "# print(y.grad)"
   ],
   "id": "b3dec1c77946176a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 二次方程拟合",
   "id": "84b761d96a2e26ce"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import torch"
   ],
   "id": "de0fb6389152172e",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 模拟真实数据 y = 2x + 1\n",
    "X = torch.linspace(0, 10, 100).unsqueeze(1)   # shape: (100, 1)\n",
    "Y = 2 * X + 1 + 0.5 * torch.randn(X.size())   # 加一些噪声\n",
    "\n",
    "plt.scatter(X, Y, label=\"Data\")\n",
    "# plt.plot(X, Y, color='red', label=\"Fitted Line\")\n",
    "plt.legend()\n",
    "plt.show()"
   ],
   "id": "4be36838788a3021",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 随机初始化 w 和 b，并开启梯度跟踪\n",
    "w = torch.randn(1, requires_grad=True)\n",
    "b = torch.randn(1, requires_grad=True)\n",
    "\n",
    "\n",
    "def model(x):\n",
    "    return w * x + b\n",
    "\n",
    "loss_fn = torch.nn.MSELoss()\n"
   ],
   "id": "65f74850d350e63a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "lr = 0.01  # 学习率\n",
    "epochs = 200\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    # 前向计算\n",
    "    y_pred = model(X)\n",
    "    loss = loss_fn(y_pred, Y)\n",
    "\n",
    "    # 反向传播\n",
    "    loss.backward()\n",
    "\n",
    "    # 参数更新（注意要在 no_grad 下）\n",
    "    with torch.no_grad():\n",
    "        w -= lr * w.grad\n",
    "        b -= lr * b.grad\n",
    "\n",
    "    # 清零梯度，否则会累积\n",
    "    w.grad.zero_()\n",
    "    b.grad.zero_()\n",
    "\n",
    "    if (epoch + 1) % 20 == 0:\n",
    "        print(f\"Epoch {epoch+1}: w={w.item():.3f}, b={b.item():.3f}, loss={loss.item():.4f}\")\n"
   ],
   "id": "dd9714e61e8b63dc",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "pred = model(X).detach()\n",
    "plt.scatter(X, Y, label=\"Data\")\n",
    "plt.plot(X, pred, color='red', label=\"Fitted Line\")\n",
    "plt.legend()\n",
    "plt.show()"
   ],
   "id": "e204c1360599c670",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import torch\n",
    "from torch.utils.data import TensorDataset, DataLoader\n",
    "\n",
    "\n"
   ],
   "id": "6775426f10f2b225",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 生成数据\n",
    "X = torch.linspace(0, 10, 100).unsqueeze(1)\n",
    "Y = 2 * X + 1 + 0.5 * torch.randn(X.size())\n",
    "\n",
    "# 使用 TensorDataset 封装\n",
    "dataset = TensorDataset(X, Y)\n",
    "\n",
    "# 使用 DataLoader 批量加载数据\n",
    "dataloader = DataLoader(dataset, batch_size=10, shuffle=True)"
   ],
   "id": "98687881c7190c70",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import torch.nn as nn\n",
    "\n",
    "class LinearRegressionModel(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.linear = nn.Linear(1, 1)  # 输入1维 -> 输出1维\n",
    "\n",
    "    def forward(self, x):\n",
    "        return self.linear(x)\n",
    "\n",
    "model = LinearRegressionModel()\n"
   ],
   "id": "ff78592da4860888",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "for name, param in model.named_parameters():\n",
    "    print(name, param.data)\n"
   ],
   "id": "75a9954ac7cc971a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import torch.optim as optim\n",
    "\n",
    "loss_fn = nn.MSELoss()             # 均方误差\n",
    "optimizer = optim.SGD(model.parameters(), lr=0.01)  # 随机梯度下降\n"
   ],
   "id": "de7d328cd2e53551",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "epochs = 200\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    for X_batch, Y_batch in dataloader:\n",
    "        # 前向传播\n",
    "        y_pred = model(X_batch)\n",
    "        loss = loss_fn(y_pred, Y_batch)\n",
    "\n",
    "        # 反向传播\n",
    "        optimizer.zero_grad()\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "\n",
    "    if (epoch + 1) % 20 == 0:\n",
    "        print(f\"Epoch {epoch+1}, Loss = {loss.item():.4f}\")\n"
   ],
   "id": "d3db4b4569de71a2",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "pred = model(X).detach()\n",
    "\n",
    "plt.scatter(X, Y, label=\"Data\")\n",
    "\n",
    "plt.plot(X, pred, color='red', label=\"Fitted Line\")\n",
    "\n",
    "plt.legend()\n",
    "plt.show()"
   ],
   "id": "d7313341bffb8320",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import torch\n",
    "import torch.optim as optim\n",
    "from torch.utils.data import TensorDataset, DataLoader\n",
    "import torch.nn as nn\n",
    "loss_fn = nn.MSELoss()"
   ],
   "id": "cea9ac70204501d8",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "X = torch.linspace(0, 1, 100).unsqueeze(1)\n",
    "Y = X ** 6\n",
    "\n",
    "dataset = TensorDataset(X, Y)\n",
    "\n",
    "dataloader = DataLoader(dataset, batch_size=10, shuffle=True)\n",
    "\n",
    "plt.scatter(X, Y, label=\"Data\")\n",
    "plt.legend()\n",
    "plt.show()\n",
    "# plt.plot(X, pred, color='red', label=\"Fitted Line\")\n"
   ],
   "id": "566649fdd8c065d0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "class myModel(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.net = nn.Sequential(\n",
    "            nn.Linear(1, 64),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(64, 64),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(64, 1)\n",
    "        )\n",
    "\n",
    "    def forward(self, x):\n",
    "        return self.net(x)\n",
    "\n",
    "model = myModel()\n"
   ],
   "id": "6345748426c756a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "loss_fn = nn.MSELoss()             # 均方误差\n",
    "optimizer = optim.SGD(model.parameters(), lr=0.01)  # 随机梯度下降"
   ],
   "id": "1c82be7685d9114a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "epochs = 2000\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    for X_batch, Y_batch in dataloader:\n",
    "        # 前向传播\n",
    "        y_pred = model(X_batch)\n",
    "        loss = loss_fn(y_pred, Y_batch)\n",
    "\n",
    "        # 反向传播\n",
    "        optimizer.zero_grad()\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "\n",
    "    if (epoch + 1) % 200 == 0:\n",
    "        print(f\"Epoch {epoch + 1}, Loss = {loss.item():.4f}\")\n",
    "        pred = model(X).detach()\n",
    "        plt.scatter(X, Y, label=\"Data\")\n",
    "        plt.plot(X, pred, color='red', label=\"Fitted Line\")\n",
    "        plt.legend()\n",
    "        plt.show()\n"
   ],
   "id": "8471cd67a743447d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "\n",
    "pred = model(X).detach()\n",
    "plt.scatter(X, Y, label=\"Data\")\n",
    "plt.plot(X, pred, color='red', label=\"Fitted Line\")\n",
    "plt.legend()\n"
   ],
   "id": "501d7924b59e7ae6",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
